#!/bin/bash

#本地机安装 sudo apt-get install sshpass
# 这个脚本可以用
# 定义远程主机信息
REMOTE_USER="sukai"
REMOTE_PASS="sukai"
REMOTE_HOST="10.9.162.109"

# 使用sshpass登录远程主机并获取其公钥
REMOTE_PUB_KEY=$(sshpass -p "${REMOTE_PASS}" ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'cat ~/.ssh/id_rsa.pub')

# 检查是否成功获取公钥
if [ -n "$REMOTE_PUB_KEY" ]; then
    echo "Successfully retrieved public key from remote host."
    # 打印公钥
    echo "Public Key:"
    echo "${REMOTE_PUB_KEY}"

    # 追加公钥到本地的 authorized_keys 文件中
    echo "${REMOTE_PUB_KEY}" >> ~/.ssh/authorized_keys
    echo "Public key added to local authorized_keys."
else
    echo "Failed to retrieve the public key."
fi




